/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.erp.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author jeeplus
 * @version 2014-4-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	/**
	 * 获取年月
	 */
	public static String getYearMonth(Date date) {

		return formatDate(date,"yyyy")+formatDate(date,"MM");
	}

	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	public static String getDateTimeNum(){
		String date=formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
		date= StringUtils.replace(date,"-","");
		date= StringUtils.replace(date,":","");
		date= StringUtils.replace(date," ","");

		return date;
	}
	public static String getYearMonthDayNum(){
		String date=formatDate(new Date(), "yyyy-MM-dd");
		date= StringUtils.replace(date,"-","");

		return date;
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	public static String getYear(Date date) {
		return formatDate(date, "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * 日期型字符串转化为日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000L);
	}


	/**
	 * 之后多少天的时间
	 * @param days
	 * @return
	 */
	public static long getAfterDays(int days){
		long t=new Date().getTime()+days*24*60*60*1000L;
		return t;
	}
	public static Date getDateAfterDays(Date d,int days){
		long t=d.getTime()+days*24*60*60*1000L;
		Date afterTime=new Date(t);
		return afterTime;
	}
	/**
	 * 指定时间之后多少天的时间
	 * @param days
	 * @return
	 */
	public static long getAfterDays(Date d,int days){
		long t=d.getTime()+days*24*60*60*1000L;
		return t;
	}

	public static long diffDate(Date nowTime,Date extTime){
	long time=	nowTime.getTime()-extTime.getTime();
	return time;
	}

	/**
	 * 之前多少天的时间
	 * @param days
	 * @return
	 */
	public static long getBeforeDays(int days){
		long t=new Date().getTime()-days*24*60*60*1000L;
		return t;
	}

	/**
	 * 指定时间之前多少天的时间
	 * @param days
	 * @return
	 */
	public static long getBeforeDays(Date d,int days){
		long t=d.getTime()-days*24*60*60*1000L;
		return t;
	}
	public static Date getBeforeDaysDate(Date d,int days){
		long t=d.getTime()-days*24*60*60*1000L;
		Date beforeDays=new Date(t);
		return beforeDays;
	}
	/**
	 *
	 * @param d
	 * @param days
	 * @return
	 */
	public static long getSpecificAfterDays(Date d,int days){
		long t=d.getTime()+days*24*60*60*1000L;
		return t;
	}
	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000L);
	}

	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000L);
	}

	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastSecond(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(1*1000L);
	}

	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000L);
		long hour = (timeMillis/(60*60*1000L)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }

	/**
	 * 时长
	 * @param timeMillis
	 * @return
	 */
	public static String formatDateTimeToString(long timeMillis){

		long day = timeMillis/(24*60*60*1000L);
		long hour = (timeMillis/(60*60*1000L)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+"天":"")+(hour>0?hour+"时":"")+(min>0?min+"分":"")+(s>0?s+"秒":"");
	}

	/**
	 * 过去多少时间转换为时间（天,时:分:秒.毫秒）
	 * @param timeMillis
	 * @return
	 */
	public static String pastTime(long timeMillis){
		long tempTimeMillis=timeMillis;
		timeMillis=new Date().getTime()-timeMillis;
		long day = timeMillis/(24*60*60*1000L);
		long hour = (timeMillis/(60*60*1000L)-day*24);
		long min = ((timeMillis/(60*1000L))-day*24*60-hour*60);
		long s = (timeMillis/1000L-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000L);
		if(day>31){
			return DateUtils.formateDate("yyyy-MM-dd",tempTimeMillis);
		}
		if(day>0){
			return day+"天前";
		}
		if(hour>0){
			return hour+"小时前";
		}
		if(min>0){
			return min+"分钟前";
		}
		return "刚刚";
	}
	/**
	 * 过去的时间

	 * @return
	 */
	public static String pastTimeForDate(Date date){

		long timeMillis=new Date().getTime()-date.getTime();
		long day = timeMillis/(24*60*60*1000L);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		if(day>31){
			return DateUtils.formatDate(date,"yyyy-MM-dd");
		}
		if(day>0){
			return day+"天前";
		}
		if(hour>0){
			return hour+"小时前";
		}
		if(min>0){
			return min+"分钟前";
		}
		return "刚刚";
	}

	/**
	 * 时长
	 * @param timeMillis
	 * @return
	 */
	public static String formatDateToHHMM(long timeMillis){
		long day = timeMillis/(24*60*60*1000L);
		long hour = (timeMillis/(60*60*1000));
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (hour>0?hour+"时":"0时")+(min>0?min+"分":"0分");
	}
    public static String formateDate(String dateFormat,long timeMillis){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		      Date date = new Date(timeMillis);
		     return sdf.format(date);
	}

	/**
	 * 获取两个日期之间的天数
	 *
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24L);
	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		long timelong=0;
	System.out.println(	DateUtils.formatDateTime(timelong));
	System.out.println(DateUtils.formatDateToHHMM(timelong));
	}
}
